package com.ling.travels.service;

import com.ling.travels.dao.UserDao;
import com.ling.travels.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 编程之外
 * @company lingStudy
 * @create 2020-06-28 20:06
 */
@Service
@Transactional  // 事务
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    //登录
    @Override
    public User login(User user) {
        //根据用户名查询数据库中的用户信息
        User userDB = userDao.findByUsername(user.getUsername());
        if (userDB!=null){
            //如果数据库中的密码与前端传过来的user.getPassword相等
            if (userDB.getPassword().equals(user.getPassword())){
                return userDB;
            }
            // RuntimeException 打印在后端控制台
            throw new RuntimeException("密码错误~~~");
        }else {
            throw new RuntimeException("用户名不存在！");
        }
    }

    //注册
    @Override
    public void register(User user) {
        if (userDao.findByUsername(user.getUsername())==null){
            userDao.saveUser(user);
        }else {
            throw new RuntimeException("用户名已存在~~~");
        }

    }
}
